﻿using System;
using System.Collections.Generic;
using Dict.DataModel;
using Dict.Blo;
using Dict.Config;

namespace UI
{
    public class UITreasure
    {
        public TableTreasure treasureData;
        public TableTreasureRefine currentRefineData;
        public TableTreasureStrengthen currentStrengthenData;

        private BaseAttributeData currentRefinePercentAttribute;
        private BaseAttributeData currentRefineAttribute;
        private BaseAttributeData currentStrenthenAttribute;

        public string guid;
        public string treasureId;
        public int strengthLevel;//强化
        public int currentExp;
        //public int totalExp;

        public string name
        {
            get
            {
                return TableTextBlo.GetText("name_"+treasureId);
            }

        }
        public string iconPath
        {
            get
            {
                if (treasureData != null)
                {
                    return "TreasureIcons/" + treasureData.icon;
                }
                else
                {
                    return null;
                }
            }
        }

        public string PicturePath
        {
            get
            {
                if (treasureData != null)
                {
                    return "Treasure2d/" + treasureData.png;
                }
                else
                {
                    return null;
                }
            }
        }


        public int refineLevel;//精炼


        private BaseAttributeData currentAttribute;

        #region 辅助
        public UIHeroFormation owner;
        public UIInstPlayerData simplePlayer;
        #endregion

        #region 初始化
        public UITreasure(string id, UIInstPlayerData simplePlayer)
        {
            this.simplePlayer = simplePlayer;
            this.treasureData = DictTreasureBlo.GetTableTreasure(id);
            if (treasureData == null)
            {
                throw new System.NullReferenceException("treasureId:" + id);
            }
            this.currentRefineData = DictTreasureRefineBlo.GetTableTreasureRefine(id);
            this.currentStrengthenData = DictTreasureStrengthBlo.GetTableTreasureStrengthen(id);
            this.currentRefineAttribute = new BaseAttributeData(0);
            this.currentRefinePercentAttribute = new BaseAttributeData(0);
            this.currentStrenthenAttribute = new BaseAttributeData(0);
            this.currentAttribute = new BaseAttributeData(0);
        }

        public UITreasure(string id,bool resetAttribute)
        {
            this.treasureData = DictTreasureBlo.GetTableTreasure(id);
            if(treasureData == null)
            {
                UnityEngine.Debug.LogError("treasure id：" + id + " not exist!");
                return;
            }
            this.treasureId = treasureData.id;
            this.strengthLevel = 1;
            this.refineLevel = 0;
            this.currentExp = 0;
            this.currentRefineData = DictTreasureRefineBlo.GetTableTreasureRefine(treasureId);
            this.currentStrengthenData = DictTreasureStrengthBlo.GetTableTreasureStrengthen(treasureId);
            this.currentRefineAttribute = new BaseAttributeData(0);
            this.currentRefinePercentAttribute = new BaseAttributeData(0);
            this.currentStrenthenAttribute = new BaseAttributeData(0);
            this.currentAttribute = new BaseAttributeData(0);
            if (resetAttribute)
            {
                UpdateRefineAttribute();
                UpdateStrenthAttribute();
                ResetAttribute();
            }
        }

        public UITreasure(ProtoBuf.Message.TreasureSimpleInfo data)
        {
            treasureId = data.treasureId;
            treasureData = DictTreasureBlo.GetTableTreasure(data.treasureId);
            strengthLevel = data.strengthLevel;
            refineLevel = data.refineLevel;
        }

        public void InitData(ProtoBuf.Message.Treasure data)
        {
            this.guid = data.guid;
            this.treasureId = data.treasureId;
            this.strengthLevel = data.strengthLevel;
            this.refineLevel = data.refineLevel;
            this.currentExp = data.currentExp;
            //this.totalExp = data.totalExp;

            currentRefineAttribute.Reset();
            currentRefinePercentAttribute.Reset();
            currentStrenthenAttribute.Reset();
            currentAttribute.Reset();

            UpdateRefineAttribute();
            UpdateStrenthAttribute();
            ResetAttribute();
        }

        private void UpdateRefineAttribute()
        {
            if (this.currentRefineData != null)
            {
                this.currentRefinePercentAttribute.SetProp(AttributeQueue.Attack, this.currentRefineData.perAttGrow * refineLevel * CommonMethod.tableAttributeConvert);
                this.currentRefinePercentAttribute.SetProp(AttributeQueue.Hp, this.currentRefineData.perHpGrow * refineLevel * CommonMethod.tableAttributeConvert);
                this.currentRefineAttribute.SetProp(AttributeQueue.cri, this.currentRefineData.criGrow * refineLevel);
                this.currentRefineAttribute.SetProp(AttributeQueue.antiCri, this.currentRefineData.anticriGrow * refineLevel);
                this.currentRefineAttribute.SetProp(AttributeQueue.hit, this.currentRefineData.perhitGrow * refineLevel);
                this.currentRefineAttribute.SetProp(AttributeQueue.dodge, this.currentRefineData.perevadeGrow * refineLevel);
            }
            var tsa = DictTreasureSkillBlo.GetTableTreasureSkillsAttribute(treasureId, refineLevel);
            char[] sp = { ',' };
            for (int i = 0; i< tsa.Count;++i)
            {
                if (!string.IsNullOrEmpty(tsa[i].bonus))
                {
                    string[] prop = tsa[i].bonus.Split(sp, System.StringSplitOptions.RemoveEmptyEntries);
                    currentRefineAttribute.AddProp(prop[0], double.Parse(prop[1]));
                }
            }
        }

        private void UpdateStrenthAttribute()
        {
            if (this.currentStrengthenData != null)
            {
                this.currentStrenthenAttribute.SetProp(AttributeQueue.Attack, this.treasureData.att + this.currentStrengthenData.attGrow * (strengthLevel - 1));
                this.currentStrenthenAttribute.SetProp(AttributeQueue.Hp, this.treasureData.hp + this.currentStrengthenData.hpGrow * (strengthLevel - 1));
                this.currentStrenthenAttribute.SetProp(AttributeQueue.cri, this.treasureData.cri + this.currentStrengthenData.criGrow * (strengthLevel - 1));
                this.currentStrenthenAttribute.SetProp(AttributeQueue.antiCri, this.treasureData.anticri + this.currentStrengthenData.anticriGrow * (strengthLevel - 1));
                this.currentStrenthenAttribute.SetProp(AttributeQueue.hit, this.treasureData.perhit + this.currentStrengthenData.perhitGrow * (strengthLevel - 1));
                this.currentStrenthenAttribute.SetProp(AttributeQueue.dodge, this.treasureData.perevade + this.currentStrengthenData.perevadeGrow * (strengthLevel - 1));
            }
        }
        #endregion

        #region Tool

        public bool IsZeroTreasure()
        {
            if(strengthLevel == 1 && refineLevel == 0)
            {
                return true;
            }
            return false;
        }

        private void ResetAttribute(bool board = true)
        {
            currentAttribute.Reset();
            currentAttribute.AddProp(currentRefineAttribute);
            currentAttribute.AddProp(currentStrenthenAttribute);
        }

        public double GetProp(AttributeQueue propId)
        {
            return currentAttribute.GetProp(propId);
        }
        public BaseAttributeData CurrentAttribute
        {
            get
            {
                return currentAttribute;
            }
        }
        public BaseAttributeData PercentAttribute
        {
            get
            {
                return currentRefinePercentAttribute;
            }
        }

        public int GetTreasureProvideExp()
        {
            int totalexp = DictTreasureStrengthBlo.GetTotalNeedExpToLevel(treasureData.quality, strengthLevel);
            totalexp += currentExp;
            totalexp += treasureData.exp;
            return totalexp;
        }

        public int GetTreasureSellCoin()
        {
            int totalexp = DictTreasureStrengthBlo.GetTotalNeedExpToLevel(treasureData.quality, strengthLevel);
            totalexp += currentExp;
            totalexp += treasureData.sellPrice;
            return totalexp;
        }

        /// <summary>
        /// 获取某一属性在强化等级X时的数值，X需大于等于当前等级
        /// </summary>
        /// <param name="propId"></param>
        /// <param name="level"></param>
        /// <returns></returns>
        public float GetTreasurePropInLevel(AttributeQueue propId, int level)
        {
            float prop = 0;

            switch (propId)
            {
                case AttributeQueue.Attack:
                    prop = level * this.currentStrengthenData.attGrow;
                    break;
                case AttributeQueue.Hp:
                    prop = level * this.currentStrengthenData.hpGrow;
                    break;
                case AttributeQueue.cri:
                    prop = (float)(level * this.currentStrengthenData.criGrow);
                    break;
                case AttributeQueue.antiCri:
                    prop = (float)(level * this.currentStrengthenData.anticriGrow);
                    break;
                case AttributeQueue.hit:
                    prop = (float)(level * this.currentStrengthenData.perhitGrow);
                    break;
                case AttributeQueue.dodge:
                    prop = (float)(level * this.currentStrengthenData.perevadeGrow);
                    break;
            }

            return prop;
        }

        public float GetTreasurePropInRefineLevel(AttributeQueue propId, int level)
        {
            float prop = 0;

            switch (propId)
            {
                case AttributeQueue.Attack:
                    prop = (float)(level * this.currentRefineData.perAttGrow);
                    break;
                case AttributeQueue.Hp:
                    prop = (float)(level * this.currentRefineData.perHpGrow);
                    break;
                case AttributeQueue.cri:
                    prop = (float)(level * this.currentRefineData.criGrow);
                    break;
                case AttributeQueue.antiCri:
                    prop = (float)(level * this.currentRefineData.anticriGrow);
                    break;
                case AttributeQueue.hit:
                    prop = (float)(level * this.currentRefineData.perhitGrow);
                    break;
                case AttributeQueue.dodge:
                    prop = (float)(level * this.currentRefineData.perevadeGrow);
                    break;
            }

            return prop;
        }

        public List<AttributeQueue> GetStrengthenPropIds()
        {
            List<AttributeQueue> propIds = new List<AttributeQueue>();
            if (this.currentStrengthenData.attGrow > 0)
            {
                propIds.Add(AttributeQueue.Attack);
            }
            if (this.currentStrengthenData.criGrow > 0)
            {
                propIds.Add(AttributeQueue.cri);
            }
            if (this.currentStrengthenData.hpGrow > 0)
            {
                propIds.Add(AttributeQueue.Hp);
            }
            if (this.currentStrengthenData.anticriGrow > 0)
            {
                propIds.Add(AttributeQueue.antiCri);
            }
            if (this.currentStrengthenData.perhitGrow > 0)
            {
                propIds.Add(AttributeQueue.hit);
            }
            if (this.currentStrengthenData.perevadeGrow > 0)
            {
                propIds.Add(AttributeQueue.dodge);
            }

            return propIds;
        }

        public List<AttributeQueue> GetRefinePropIds()
        {
            List<AttributeQueue> propIds = new List<AttributeQueue>();
            if (this.currentRefineData.perAttGrow > 0)
            {
                propIds.Add(AttributeQueue.Attack);
            }
            if (this.currentRefineData.criGrow > 0)
            {
                propIds.Add(AttributeQueue.cri);
            }
            if (this.currentRefineData.perHpGrow > 0)
            {
                propIds.Add(AttributeQueue.Hp);
            }
            if (this.currentRefineData.anticriGrow > 0)
            {
                propIds.Add(AttributeQueue.antiCri);
            }
            if (this.currentRefineData.perhitGrow > 0)
            {
                propIds.Add(AttributeQueue.hit);
            }
            if (this.currentRefineData.perevadeGrow > 0)
            {
                propIds.Add(AttributeQueue.dodge);
            }

            return propIds;
        }
        /// <summary>
        /// 饰品品质名字“x色xx”
        /// </summary>
        /// <returns></returns>
        public string GetQualityWord()
        {
            int type = (treasureData.type + 2);
            string str = "";
            str += TableTextBlo.GetText("quality_color_" + treasureData.quality.ToString()).Replace("\\r", "\r").Replace("\\n", "\n");
            str += TableTextBlo.GetText("treasure_type_" + type.ToString()).Replace("\\r", "\r").Replace("\\n", "\n");
            return str;
        }
        #endregion
    }
}
